package tv.accedo.memory.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import tv.accedo.memory.dao.UserDao;
import tv.accedo.memory.model.User;

public class HiServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8"); 
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter(); 
		System.out.println("Server get one request from client side!"); 
		
		List<User> userList = new ArrayList<User>(); 
		UserDao userDao = new UserDao(); 
		userList = userDao.getAllUsers();  
		User user = new User();
		String jsonString = "]}"; 
		for (Iterator<User> iter = userList.iterator(); iter.hasNext(); ) {
			user = iter.next(); 
			jsonString = "{username:'" + user.getUsername() + "',score:'" + user.getScore() + "'}," + jsonString; 
		}
		jsonString = "{hiscore:[" + jsonString; 
//		System.out.println(jsonString); 
		out.print(jsonString); 
		
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}